package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.pojo.TbItemCat;
import com.dongdong.service.ItemService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemContoller")
public class ItemCatContoller {
    @Reference(timeout = 3000)
    private ItemService itemService;
    @RequestMapping("toCat")
    public String toCat(Model model){
        List<TbItemCat> itemList = itemService.selectAll();
        model.addAttribute("itemList",itemList);
        return "admin/item_cat";
    }
    @RequestMapping("itemCatList")
    @ResponseBody
    public Result itemCatList(Long parentId) {
        try {
            List<TbItemCat> list = itemService.selectInfo(parentId);
            return Result.respone(ResultEmnu.SUCCESS).setData(list);

        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("increase")
    @ResponseBody
    public Result increase(TbItemCat tbItemCat){
        int i = itemService.insert(tbItemCat);
        if (i > 0){
            return Result.respone(ResultEmnu.SUCCESS);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id")Long id){
        try {
                TbItemCat tbItemCat = itemService.selectById(id);
                return Result.respone(ResultEmnu.SUCCESS).setData(tbItemCat);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbItemCat tbItemCat){
        int i = itemService.updateInfo(tbItemCat);
        if (i > 0){
            return Result.respone(ResultEmnu.SUCCESS);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("deleteBatch/{ids}")
    @ResponseBody
    public Result deleteBatch(@PathVariable("ids")Long[] ids){
       int  i =  itemService.deleteBatch(ids);
       if (i > 0){
           return Result.respone(ResultEmnu.SUCCESS);
       }else {
           return Result.respone(ResultEmnu.ERROR);
       }
    }



}
